<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\file\FileInput;
use \kucha\ueditor\UEditor;
use common\models\fenhuiartic\Fenhuiartic;
use common\models\fenhuicat\Fenhuicat;
use common\models\fenhuigrade\Fenhuigrade;
use yii\helpers\ArrayHelper;
use common\models\modelled\Modelled;
/* @var $this yii\web\View */
/* @var $model common\models\fenhuiartic\Fenhuiartic */
/* @var $form yii\widgets\ActiveForm */


?>
<?php $this->beginBlock('siderbar'); ?>
<?= $this->render('//layouts/fenhui-menu') ?>
<?php $this->endBlock(); ?>
<?php $parentCatalog = ArrayHelper::merge([0 =>'--请选择分会--'], Fenhuicat::getArrayFenhuigrade());unset($parentCatalog[$model->id]);
?>
<?php  $parent = ArrayHelper::merge([0 =>'顶级分类'], ArrayHelper::map(Fenhuicat::get(0, Fenhuicat::find()->where(['fg_id'=>$model->fg_id])->all()), 'id', 'str_label'));unset($parentCatalog[$model->id]); ?>
<style>
.kv-file-content img{ width: 100%!important; }
</style>
<script type="text/javascript" src="<?= yii::getAlias('@web');?>/js/jquery.js"></script>
<div class="fenhuiartic-form">
    <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data'],'id'=>'subform']); ?>
    <!--去掉了where（['status']=>1）-->
    <?= $form->field($model, 'fg_id')->dropDownList($parentCatalog,
        [
            'onchange' => '
            $.post("' . yii\helpers\Url::to(['fenhuicat/getfc/']) . '/"+$(this).val(),function(data){
                $("#fenhuiartic-fc_id").html(data);
            });',
        ]) ?>
    <?= $form->field($model, 'fc_id')->dropDownList($parent) ?>
    <div class="row">
        <div class="col-md-4">
            <?= $form->field($model, 'title')->textInput(['maxlength' => 30, 'oninput' => "myFunction()"]) ?>
        </div>
        <div class="col-md-4">
            <label>标题还可输入字符数</label>
            <input id="titlenum" type="text" class="form-control" value="30" />
            <input id="cont" name="Fenhuiartic[cont]" type="hidden" class="form-control" value="" />
            <script>
                function myFunction() {
                    var p = $("#fenhuiartic-title").val().trim();
                    var len = 30 - p.length;
                    $("#titlenum").val(len);
                }
            </script>
        </div>

    </div>
    <?= $form->field($model, 'subhead')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'intro')->textarea(['rows' => 6]) ?>

    <?php
    if(isset(json_decode($model->img)->newname)){
        $img = json_decode($model->img)->newname;
    }else{

        $img = $model->img;
    }

    $siteRoot = Yii::$app->params['qtimg'];
    echo '<label class="control-label">图片</label>;';
    echo FileInput::widget([
        'model' => $model,
        'attribute' => 'img',
        'options' => ['accept' => 'image/*'],
        'pluginOptions' => [
            'uploadExtraData' => [
                'album_id' => 20,
                'cat_id' => 'Nature'
            ],
            'initialPreview'=>[
                !empty($img)?Html::img(Yii::$app->params['qtimg'].$img):null,
            ],
            'initialCaption'=> $img,
            'maxFileCount' => 10,
            "showUpload"=> false,
        ]
    ]);
    ?>

    <?= $form->field($model, 'author')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'comefrom')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'link')->textInput(['maxlength' => true]) ?>
    <?php
    if(isset($modelled)){
        foreach($modelled as $key=>$val){
            $valname = '';
            $decode = json_decode($model->optiontag,true);
            if($decode){
                $keyiset = array_key_exists($val['name'], $decode);

                if($keyiset){
                    $valname = $decode[$val['name']];
                }
            }

            echo  Modelled::createform($val['option'],$val['name'],'tag['.$val['name'].']',  $valname);
        }
    }

    ?>
    <?=
    $form->field($model,'content')->widget('kucha\ueditor\UEditor',[]);
    ?>

    <div class="row">

        <div class="col-md-4">
            <input id="summarycheck" type="checkbox" class="dese" >
            <label>是否截取内容</label>
            <input class="fontsize" type="text" value="200" style="width: 40px;" />
            <label>字符至内容摘要</label>
        </div>
        <script>
            $(document).ready(function(){
                $(".dese").click(function(){

                    if($('.dese').is(':checked')) {
                        var txt = $('textarea[name="Fenhuiartic[content]"]').val();
                        var start_ptn = /<\/?[^>]*>/g;
                        var end_ptn = /[ | ]*\n/g;
                        var space_ptn = /&nbsp;/ig;
                        var newtxt = txt.replace(start_ptn,"").replace(end_ptn).replace(space_ptn,"");
                        var SIZE = $(".fontsize").val();
                        $("#fenhuiartic-intro").val(newtxt.substr(0,SIZE));
                    };

                })
            }) ;
        </script>
        <div class="col-md-4">
            <input id="cover" name="Fenhuiartic[cover]" type="hidden" value="" />
            <input id="covercheck" type="checkbox" class="coverdese" >
            <label>是否截取内容第</label>
            <input class="coverfontsize" type="text" value="1" style="width: 20px;" />
            <label>张图片作为标题图片</label>
        </div>
        <script>
            $(document).ready(function(){
                $(".coverdese").click(function(){
                    if($('.coverdese').is(':checked')) {
                        var index = $(".coverfontsize").val();
                        var txt = $('textarea[name="Fenhuiartic[content]"]').val();
                        var reg = /src=\"[^>]*\"\/>/gi;
                        var res = txt.match(reg);

                        if(res != null){
                            var leng = txt.match(reg)['length'];

                            if((index-1) < leng){
                                var b = res[(index-1)];
                                console.log(b);
                                $("#cover").val(b);

                            } else {
                                alert("没有第 "+index+" 张图片");
                                $("#covercheck").attr("checked", false);
                            }
                        } else {
                            alert("没有第 "+index+" 张图片");
                            $("#covercheck").attr("checked", false);
                        }
                    };
                })
            }) ;
        </script>
    </div>

    <?= $form->field($model, 'seokeyword')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'seocontent')->textarea(['rows' => 6]) ?>

    <?= $form->field($model, 'created_at')->textInput(['value' => date('Y-m-d H:i:s', time())]) ?>

    <?= $form->field($model, 'isrecom')->dropDownList(['是' => '是', '否' => '否']) ?>

    <?= $form->field($model, 'push')->dropDownList(['1' => '是', '0' => '否']) ?>

    <?= $form->field($model, 'status')->dropDownList(Fenhuiartic::getArrayStatus()) ?>


    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
        <?php if($model->isNewRecord) { ?>
            <?= Html::Button(Yii::t('app', '新增后继续添加'), ['class' => 'btn btn-success', 'id' => 'continue']) ?>
        <?php } ?>
    </div>
    <script type="text/javascript">
        $(document).ready(function(){
            $("#continue").click(function(){
                $("#cont").val(1);
                var ttt = $("#cont").val();
                $("#subform").submit();
            })

        });
    </script>

    <?php ActiveForm::end(); ?>

</div>
